package com.hecn.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author 陈朋康
 * @site hecn.gitee.io
 * @discription 通过设置ServletAPI , 不需要视图解析器.
 */
@Controller
public class ResultGo {

    /**
     * @param req
     * @param rsp
     * @throws IOException 输出
     * @discription 1.通过HttpServletResponse进行输出
     */
    @RequestMapping("/result/t1")
    public void test1(HttpServletRequest req, HttpServletResponse rsp) throws IOException {
        rsp.getWriter().println("使用ServletAPI");
    }


    /**
     * @param req
     * @param rsp
     * @throws IOException 重定向
     * @discription 2. 通过HttpServletResponse实现重定向
     */
    @RequestMapping("/result/t2")
    public void test2(HttpServletRequest req, HttpServletResponse rsp) throws IOException {
        rsp.sendRedirect("/index.jsp");
    }


    /**
     * 转发
     *
     * @param req
     * @param rsp
     * @throws ServletException
     * @throws IOException
     * @discription 3. 通过HttpServletResponse实现转发
     */
    @RequestMapping("/result/t3")
    public void test3(HttpServletRequest req, HttpServletResponse rsp) throws ServletException, IOException {
        req.setAttribute("msg", "/result/t3");
        req.getRequestDispatcher("/WEB-INF/jsp/test.jsp").forward(req, rsp);
    }
}
